package com.siyuanren.gcocp.input;

import com.siyuanren.gcocp.entity.Part;
import com.siyuanren.gcocp.entity.Word;
import com.siyuanren.gcocp.mapper.base.PartMapper;
import com.siyuanren.gcocp.mapper.base.WordMapper;
import com.siyuanren.gcocp.mapper.base.WordPartRelateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by   anxing
 * Project_name wordGame
 * Path         com.woodstar.word
 * Date         2018/6/19
 * Time         16:21
 * Description
 */
@Component
public class WordParse {
    @Autowired
    private PartMapper partMapper;
    @Autowired
    private WordMapper wordMapper;
    @Autowired
    private WordPartRelateMapper wordPartRelateMapper;

    boolean isPartFirst = false;  // // 判断词根开始行: 120、
    boolean isWord = false;   // 是否为单词
    boolean isExplain = false;  // 是否为解释
    boolean isTranslate = false; // 是否为翻译
    int partNumber = 1;

    List<String> parts = new ArrayList<>();
    List<String> partTrans = new ArrayList<>();
    List<String> words = new ArrayList<>();
    List<String> wordExplains = new ArrayList<>();
    List<String> wordTrans = new ArrayList<>();
    Map<String,List<String>> wordParts = new HashMap<>();
    /**
     * 当前正在处理的词缀
     */
    String currPart;
    Integer currPartId;


    public boolean parsePartFirt(String line) throws Exception {
        boolean parse = false;
        String partFirstEx = ".*(\\d+、)([a-zA-Z]+).*";
        if (Pattern.matches(partFirstEx, line)) {
            // 是否包含中文
            Pattern pattern = Pattern.compile(".*([a-zA-Z\\(\\)]+).?[\\u2E80-\\u9FFF]{1,}.*$");
            Matcher matcher = pattern.matcher(line);
            if (matcher.find()) {
                // 删除数字
                line = line.replaceAll("\\d+、", "");
                cutPartExplain(line);
            } else { // 没有包含中文， 词根含义在下一行
                isPartFirst = true;
            }
            parse = true;
        } else if (isPartFirst) {
            isPartFirst = false;
            cutPartExplain(line);
            parse = true;
        }
        return parse;
    }

    public void cutPartExplain(String part) throws Exception {
        currPart = part;
        String reg = "([a-zA-Z\\(\\)\\，]+)";
        String delreg = "([a-zA-Z\\(\\)\\，]+)[=\\s]*";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(part);
        if (matcher.find()) {
            System.out.println(matcher.group() + "-------" + part.replaceFirst(delreg, ""));
            this.parts.add(matcher.group());
            this.partTrans.add(part.replaceFirst(delreg, ""));
            Part wordPart = new Part();
            wordPart.setPart(matcher.group());
            wordPart.setPartTrans(part.replaceFirst(delreg, ""));
            partMapper.insertSelective(wordPart);
            currPartId = wordPart.getId();
        } else {
            System.out.println(part + "---------------------------------------------- not find");
            throw new Exception("解析词根失败：" + part);
        }
    }

    /**
     * 解析词语
     * @param line
     */
    public boolean parseWord(String line) {
        String reg = "\\[.*\\]";
        String word;
        String explain;
        String translate;

        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
            explain = matcher.group(0);
            explain = explain.substring(1, explain.length()-1);
            line = line.replaceFirst(reg, "<>");
            String[] arr = line.split("<>");
            if (arr.length == 2) {
                word = arr[0].trim();
                translate = arr[1].trim();
                System.out.println(currPart + ">>>" + word + ">>>" + translate +">>>" + explain);
                this.words.add(word);
                this.wordTrans.add(translate);
                this.wordExplains.add(explain);

                Word myword = new Word();
                myword.setWord(word);
                myword.setWordTranslate(translate);
                myword.setWordExplain(explain);
                myword.setMainPartId(currPartId);
                wordMapper.insertSelective(myword);
                return true;
            }
        }
        return false;
    }

    public boolean parseWordPartRelate(String line) {
        String reg = "\\[.*\\]";
        String word;
        String explain;
        String translate;

        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
            explain = matcher.group(0);
            explain = explain.substring(1, explain.length()-1);
            line = line.replaceFirst(reg, "<>");
            String[] arr = line.split("<>");
            if (arr.length == 2) {
                word = arr[0].trim();
                translate = arr[1].trim();
                parseWordPart(word, explain);
                return true;
            }
        }
        return false;
    }

    private void parseWordPart(String word, String explain) {
        String reg = "[a-zA-Z]+";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(explain);
        while (matcher.find()) {
            System.out.println(matcher.group());
            if (parts.contains(matcher.group())) {
                if (wordParts.containsKey(word)) {
                    wordParts.get(word).add(matcher.group());
                } else {
                    List<String> parts = new ArrayList<>();
                    parts.add(matcher.group());
                    wordParts.put(word, parts);
                }
            }
        }
    }

    public void printWordParts() {
        for (String s : wordParts.keySet()) {
            StringBuffer sb = new StringBuffer(s);
            sb.append("-->");
            for (String s1 : wordParts.get(s)) {
                sb.append(s1).append(" | ");
            }
            System.out.println(sb);
        }
    }

    public static void main(String[] args) {
//        Pattern pattern = Pattern.compile(".*([a-zA-Z\\(\\)]+).?[\\u2E80-\\u9FFF]{1,}.*$");
//        Matcher matcher = pattern.matcher("231、sol②太阳");
//        Pattern pattern = Pattern.compile("([a-zA-Z\\(\\)\\，]+)");
//        Matcher matcher = pattern.matcher("audi=hear 听 （audi也作audit）");
//        Pattern pattern = Pattern.compile("([a-zA-Z\\(\\)\\，]+)[=\\s]*");
//        Matcher matcher = pattern.matcher("sat，satis，satur=足，满，饱");
//        System.out.println(matcher.group(0) + "-------" + "sat，satis，satur=足，满，饱".replaceFirst("([a-zA-Z\\(\\)\\，]+)[=\\s]*", ""));
//        Pattern pattern = Pattern.compile("\\[.*\\]");
//        Matcher matcher = pattern.matcher("anniversary     [ann 年 -i- 连接字母，vers 转 –ary 名词后缀；时间转了一年] 周年纪念日，周年纪念");
        Pattern pattern = Pattern.compile("[a-zA-Z]+");
        Matcher matcher = pattern.matcher("de-向下，mot移动;’使向下移动");
        if (matcher.find()) {
            System.out.println("find:" + matcher.group());
            while(matcher.find()){
                System.out.println("find:" + matcher.group());
            }
        } else {
            System.out.println("no find");
        }

    }
}
